/**
 * 扑克类叫分
 */
var _ = require('../../FrameWork/we_underscore');
var EnumPkRoom = require('../Enum/EnumPkRoom');
var ClientDelegate = require('../../FrameWork/Delegate/ClientDelegate');
var ClientPkDelegateType = require('../Delegate/ClientPkDelegateType');
var UtilGameObject = require('./../../FrameWork/Utils/UtilGameObject');
var PkSound = require('./../PkSound');
var PkCardTool = require('./../FindTool/PkCardTool');
module.exports = cc.Class({
    extends: ClientDelegate,
    properties: {
        prefabShowOutCard: {
            default: null,
            type: cc.Prefab,
            displayName: '出牌表现单个牌',
        },
        posOutEnd: [cc.Node], //出牌目的地
    },
    onLoad() {
        this._super();
        this.ctrSelfList = [
            [],
            [],
            [],
            []
        ];
        this.addListerDelegate(ClientPkDelegateType.TestRobotAddHandCards, this.addHandsCard.bind(this));
        this.addListerDelegate(ClientPkDelegateType.TestRobotRmHandCards, this.rmHandCard.bind(this));
        this.addListerDelegate(ClientPkDelegateType.TestRobotHandCardsRmAll, this.rmAllCard.bind(this));

        this.resetNet();
        this.iniTest();
        this.showAllCard(false);
    },
    iniTest() {
        UtilGameObject.createDemoBtn([
            // ['查看牌', function() {
            //     this.showAllCard(true);
            // }.bind(this)],
            // ['隐藏', function() {
            //     this.showAllCard(false);
            // }.bind(this)],
        ]);
    },

    /***创建一张手牌 */
    createPkHandCard(_cardInfo, _seatIdx) {
        var cardCtr = UtilGameObject.getCtrByPrefabs(this.prefabShowOutCard, this.posOutEnd[_seatIdx]);
        cardCtr.setActiveLandlord(false);
        cardCtr.refreshUI(_cardInfo);
        cardCtr.setScale(0.3);
        // cardCtr.setOpacity(20);
        this.ctrSelfList[_seatIdx].push(cardCtr);
        return cardCtr;
    },

    showAllCard(_isShow) {
        _.each(this.ctrSelfList, function(list) {
            _.each(list, function(ctr) {
                if (!!ctr && ctr.node) {
                    ctr.setActive(_isShow);
                }
            })
        })
    },
    rmAllCard() {
        _.each(this.ctrSelfList, function(list) {
            _.each(list, function(ctr) {
                if (!!ctr && ctr.node) {
                    ctr.node.destroy();
                }
            })
        })

        this.ctrSelfList = [
            [],
            [],
            [],
            []
        ];
    },

    rmHandCard(jData) {
        var _seatIdx = jData.seatIdx;
        if (_seatIdx == 0) {
            return;
        }
        var cardIds = jData.cardIds;
        _.each(cardIds, function(id) {
            var findCtr = _.find(this.ctrSelfList[_seatIdx], function(ctr) {
                return id == ctr.getCardId();
            });
            findCtr.node.destroy();
            this.ctrSelfList[_seatIdx] = _.filter(this.ctrSelfList[_seatIdx], function(ctr) {
                return id != ctr.getCardId();
            })
        }.bind(this));
        this.resetSortBy(_seatIdx);
    },
    addHandsCard(jData) {
        var _seatIdx = jData.seatIdx;
        if (_seatIdx == 0) {
            return;
        }
        var cardIds = jData.cardIds;
        var cardsInfo = PkCardTool.getCardInfos(cardIds);
        cardsInfo = _.sortBy(cardsInfo, function(_cardInfo) {
            return -_cardInfo.value;
        });
        _.each(cardsInfo, function(_cardInfo, idx) {
            this.createPkHandCard(_cardInfo, _seatIdx);
        }.bind(this));
        this.showAllCard(false);
        this.resetSortBy(_seatIdx);
    },

    resetSortBy(_seatIdx) {
        this.ctrSelfList[_seatIdx] = _.each(this.ctrSelfList[_seatIdx], function(ctr) {
            return -ctr.getCardValue();
        });
        _.each(this.ctrSelfList[_seatIdx], function(ctr, idx) {
            ctr.setPositionX(-180 + idx * 20);
            ctr.setPositionY(-110);
        })
    },
    resetNet() {

    },

});